﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MathNet.Numerics;
namespace TESalgorithm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //输入温度的文本
                var strT1 = textBox1.Lines;
                if (strT1.Count() < 5) throw (new Exception("not enough object temperature data input"));
                var Tobject = strT1.Select(x=>Double.Parse(x)+273.15).Take(5);
                var strT2 = textBox2.Lines;
                if (strT2.Count() < 5) throw (new Exception("not enough atmophere radiance data input"));
                var Tatm = strT2.Select(x => Double.Parse(x)+273.15).Take(5);
                double Tguess = 0;
                //输入猜测温度
                bool success = Double.TryParse(textBox3.Text, out Tguess);
                //如果没有合法猜测温度，就使用最大的亮温值代替Tguess
                if (!success) Tguess = Tobject.Max();
                double Toutput = TES.TESalgorithm(Tobject.ToList(), out List<double> emmisivity, Tguess+273, Tatm.ToList());
                var str = emmisivity.Select(x => x.ToString()).ToList();
                var textoutput =string.Join(Environment.NewLine, str);
                textoutput = Toutput + Environment.NewLine + textoutput;
                textBox4.Text = textoutput;
                double T = Double.Parse(textBox6.Text) + 273.15;
                double T_atm = Double.Parse(textBox7.Text) + 273.15;
                //宽波段亮温读数对应的辐射
                double radianceObj= Integrate.OnClosedInterval(lambda => Equation.Plunk(lambda, T)- Equation.Plunk(lambda, T_atm), 8,12);
                //利用TES解算出来的辐射
                double radianceBB = Integrate.OnClosedInterval(lambda => Equation.Plunk(lambda, Toutput) - Equation.Plunk(lambda, T_atm), 8, 12);
                //求宽波段发射率,ε=(B_obj-B_atm)/(B_bb-B_atm)
                double WidebandEmmisivity = radianceObj / radianceBB;
                textBox5.Text = WidebandEmmisivity.ToString();
            }
            catch(Exception error)
            {
                MessageBox.Show(error.ToString());
            }
        }  
    }
}